package com.mo.lib.utils.data;


import com.mo.lib.constans.CityBean;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @ author：mo
 * @ data：2020/8/17:10:18
 * @ 功能： 排序工具类
 */
public class SortUtli {
    /**
     * 对城市列表排序
     */
    public static List<CityBean> CitySort(List<CityBean> data) {
        Collections.sort(data, new Comparator<CityBean>() {
            /** 排序规则 */
            @Override
            public int compare(CityBean o1, CityBean o2) {
                if (o1.getName().contains("定位")) {
                    return -1;
                }
                if (o2.getName().contains("定位")) {
                    return 1;
                }
                if (o1.getName().contains("热门")) {
                    return -1;
                }
                if (o2.getName().contains("热门")) {
                    return 1;
                }
                //当包含@时，我比谁都大
                if (o1.getName().contains("@")) {
                    return 1;
                }
                //当包含@时，谁都比我大
                if (o2.getName().contains("@")) {
                    return -1;
                }
                //当包含#时，我比谁都大
                if (o1.getName().contains("#")) {
                    return 1;
                }
                //当包含#时，谁都比我大
                if (o2.getName().contains("#")) {
                    return -1;
                } else {
                    if (o1.getPinyin().equals(o2.getPinyin())) {
                        return o1.getName().compareTo(o2.getName());
                    } else {
                        return o1.getPinyin().compareTo(o2.getPinyin());
                    }

                    //                    return Collator.getInstance(Locale.CHINA).compare(o1.getName(), o2.getName());
                    //                    Collator instance = Collator.getInstance(Locale.CHINA);
                    //                    return instance.getCollationKey(o1.getPinyin()).compareTo(instance.getCollationKey(o2.getPinyin()));
                }
            }
        });
        return data;
    }

    public static List<CityBean> CitySort2(List<CityBean> data) {
        HanyuPinyinOutputFormat pinyinOutputFormat = new HanyuPinyinOutputFormat();
        Collections.sort(data, new Comparator<CityBean>() {
            /** 排序规则 */
            @Override
            public int compare(CityBean o1, CityBean o2) {
                if (o1.getName().contains("定位")) {
                    return -1;
                }
                if (o2.getName().contains("定位")) {
                    return 1;
                }
                if (o1.getName().contains("热门")) {
                    return -1;
                }
                if (o2.getName().contains("热门")) {
                    return 1;
                }
                //当包含@时，我比谁都大
                if (o1.getName().contains("@")) {
                    return 1;
                }
                //当包含@时，谁都比我大
                if (o2.getName().contains("@")) {
                    return -1;
                }
                //当包含#时，我比谁都大
                if (o1.getName().contains("#")) {
                    return 1;
                }
                //当包含#时，谁都比我大
                if (o2.getName().contains("#")) {
                    return -1;
                } else {
                    String province1 = null;
                    String province2 = null;
                    try {
                        province1 = PinyinHelper.toHanYuPinyinString((String) o1.getName(), pinyinOutputFormat, " ", true);
                        province2 = PinyinHelper.toHanYuPinyinString((String) o2.getName(), pinyinOutputFormat, " ", true);
                    } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                        badHanyuPinyinOutputFormatCombination.printStackTrace();
                    }
                    return province1.compareTo(province2);

                    //                    return o1.getTopc().compareTo(o2.getTopc());
                    //                    return Collator.getInstance(Locale.CHINA).compare(o1.getName(), o2.getName());
                }
            }
        });
        return data;
    }

    public static List<CityBean> CitySort3(List<CityBean> data) {
        Collections.sort(data, new Comparator<CityBean>() {
            @Override
            public int compare(CityBean s1, CityBean s2) {
                String o1 = s1.getName();
                String o2 = s2.getName();
                for (int i = 0; i < o1.length() && i < o2.length(); i++) {

                    int codePoint1 = o1.charAt(i);
                    int codePoint2 = o2.charAt(i);

                    if (Character.isSupplementaryCodePoint(codePoint1)
                            || Character.isSupplementaryCodePoint(codePoint2)) {
                        i++;
                    }

                    if (codePoint1 != codePoint2) {
                        if (Character.isSupplementaryCodePoint(codePoint1)
                                || Character.isSupplementaryCodePoint(codePoint2)) {
                            return codePoint1 - codePoint2;
                        }

                        String pinyin1 = PinyinHelper.toHanyuPinyinStringArray((char) codePoint1) == null
                                ? null : PinyinHelper.toHanyuPinyinStringArray((char) codePoint1)[0];
                        String pinyin2 = PinyinHelper.toHanyuPinyinStringArray((char) codePoint2) == null
                                ? null : PinyinHelper.toHanyuPinyinStringArray((char) codePoint2)[0];

                        if (pinyin1 != null && pinyin2 != null) { // 两个字符都是汉字
                            if (!pinyin1.equals(pinyin2)) {
                                return pinyin1.compareTo(pinyin2);
                            }
                        } else {
                            return codePoint1 - codePoint2;
                        }
                    }
                }
                return o1.length() - o2.length();
            }
        });
        return data;
    }
}
